//
//  ViewController.m
//  UI_加速计
//
//  Created by dllo on 15/11/13.
//  Copyright (c) 2015年 沈浩. All rights reserved.
//

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
@property(nonatomic, strong)UILabel *CMAlabel; /**< 加速度 */
@property(nonatomic, strong)UILabel *CMGlabel; /**< 陀螺仪 */
@property(nonatomic, strong)UILabel *CMMlabel; /**< 磁力计 */
@property(nonatomic, strong)CMMotionManager *motionManager;
@property(nonatomic, strong)CLLocationManager *locationManager;
@end

@implementation ViewController
{
    CALayer *znzlayer;
}

// 添加 CoreMotion.framework.


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.CMAlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 375, 150)];
    self.CMAlabel.numberOfLines = 5;
    self.CMAlabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.CMAlabel];
    // 创建CMMotionManager
    self.motionManager = [CMMotionManager new];
    
    // 创建子线程来更新数据
    NSOperationQueue *queue = [NSOperationQueue new];
    
    if (self.motionManager.accelerometerAvailable) {
        // 设置CMMotionManager的加速度读更新频率
        self.motionManager.accelerometerUpdateInterval = 1;
        // 使用代码块开始获取加速度的数据
        [self.motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
            NSString *labeltext;
            if (error) {
                // 停止获取加速度数据
                [self.motionManager stopAccelerometerUpdates];
                labeltext = [NSString stringWithFormat:@"获取加速度数据错误 %@", error];
            } else {
                // 分别获取X,Y,Z轴上的加速度数据
                labeltext = [NSString stringWithFormat:@"加速度为\n-----\nX轴%+.2f \nY轴%+.2f \nZ轴%+.2f",
                             accelerometerData.acceleration.x,
                             accelerometerData.acceleration.y,
                             accelerometerData.acceleration.z];
            }
            //在主线程更新文本
            [self.CMAlabel performSelectorOnMainThread:@selector(setText:) withObject:labeltext waitUntilDone:NO];
        }];
    } else {
        NSLog(@"该设备不支持获取加速度数据");
    }
    
    self.CMAlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 375, 150)];
    self.CMAlabel.numberOfLines = 5;
    self.CMAlabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.CMAlabel];
    
    if (self.motionManager.gyroAvailable) {
        self.motionManager.gyroUpdateInterval = 0.1;
        
        //
        [self.motionManager startGyroUpdatesToQueue:queue withHandler:^(CMGyroData *gyroData, NSError *error) {
            NSString *labeltext;
            if (error) {
                [self.motionManager stopGyroUpdates];
                labeltext = [NSString stringWithFormat:@"错误%@", error];
            } else {
                labeltext = [NSString stringWithFormat:@"\n----\nX %+.2f \nY %+.2f \nZ %+.2f", gyroData.rotationRate.x, gyroData.rotationRate.y, gyroData.rotationRate.z];
            }
            [self.CMGlabel performSelectorOnMainThread:@selector(setText:) withObject:labeltext waitUntilDone:NO];
        }];
    } else {
        NSLog(@"该设备不支持获取陀螺仪数据");
    }
    
    // 磁力计
    self.CMMlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 390, 375, 150)];
    [self.view addSubview:self.CMMlabel];
    self.CMMlabel.numberOfLines = 5;
    self.CMMlabel.backgroundColor = [UIColor yellowColor];
    if (self.motionManager.magnetometerActive) {
        self.motionManager.magnetometerUpdateInterval = 0.1;
        
        [self.motionManager startMagnetometerUpdatesToQueue:queue withHandler:^(CMMagnetometerData *magnetometerData, NSError *error) {
            NSString *labeltext;
            if (error) {
                [self.motionManager stopMagnetometerUpdates];
                labeltext = [NSString stringWithFormat:@"获取磁场数据出现错误%@", error];
            } else {
                labeltext = [NSString stringWithFormat:@"\n ---\nX %+.2f \n Y%+.2f \n Z %+.2f", magnetometerData.magneticField.x, magnetometerData.magneticField.y, magnetometerData.magneticField.z];
            }
            [self.CMMlabel performSelectorOnMainThread:@selector(setText:) withObject:labeltext waitUntilDone:NO];
        }];
    } else {
        NSLog(@"不支持");
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
